package com.laozhao.msas.util;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import jp.ne.so_net.ga2.no_ji.jcom.JComException;
import jp.ne.so_net.ga2.no_ji.jcom.ReleaseManager;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelApplication;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbook;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorkbooks;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheet;
import jp.ne.so_net.ga2.no_ji.jcom.excel8.ExcelWorksheets;

public class PrintExcelByJcom {

	private static String FILE_TYPE = ".xls";
	
	/**
	 * 实线Excel打印预览
	 * 
	 * @param fileName
	 * @throws IOException 
	 * 
	 */
	public static void printPerview(String fileName,String sheetName) throws IOException {
		ReleaseManager rm = new ReleaseManager();
		checkFileType(fileName);
		try {

			// 创建可加载的组件
			ExcelApplication excel = new ExcelApplication(rm);
			// 创建工作薄对象
			ExcelWorkbooks xlBooks = excel.Workbooks();
			// 打开工作薄
			ExcelWorkbook xlBook = xlBooks.Open(fileName);
			// 打印
			ExcelWorksheets xlSheets = xlBook.Worksheets();
			ExcelWorksheet xlSheet = null;
			if (sheetName == null || "".endsWith(sheetName)) {
				xlSheet = xlBook.ActiveSheet();
			}else {
				for(int index = 1;index <= xlSheets.Count() ;index++) { 
					ExcelWorksheet sheet = xlSheets.Item(index);
					if (sheetName.equals(sheet.Name())) {
						xlSheet = sheet;
						break;
					}
				} 
				if (xlSheet == null) {
					xlSheet = xlBook.ActiveSheet();
				}
			}
			// 显示Excel
			excel.Visible(true);
			// 输出页面
			xlSheet.PrintOut(1, 1, 1, true);
			System.out.println("close");
			// 关闭
			xlBook.Close(false, null, false);
			excel.Quit();
		} catch (JComException e) {
			e.printStackTrace();
		} finally {
			rm.release();
		}
	}
	
	/**
	 * 实线Excel 打印
	 * 
	 * @param fileName
	 * 
	 */
	public static void printExcel(String fileName,String sheetName,String printName) throws IOException {
		ReleaseManager rm = new ReleaseManager();
		ExcelApplication excel = null;
		checkFileType(fileName);
		try {			
			excel = new ExcelApplication(rm);
			ExcelWorkbooks xlBooks = excel.Workbooks();
			ExcelWorkbook xlBook = xlBooks.Open(fileName);
			ExcelWorksheets xlSheets = xlBook.Worksheets();
			ExcelWorksheet xlSheet = null;
			if (sheetName == null || "".endsWith(sheetName)) {
				xlSheet = xlBook.ActiveSheet();
			}else {
				for(int index = 1;index <= xlSheets.Count() ;index++) { 
					ExcelWorksheet sheet = xlSheets.Item(index);
					if (sheetName.equals(sheet.Name())) {
						xlSheet = sheet;
						break;
					}
				} 
				if (xlSheet == null) {
					xlSheet = xlBook.ActiveSheet();
				}
			}
			
			//call one special printer to print.
			Object[] arglist = new Object[7]; 
            arglist[0] = 1; 
            arglist[1] = 1; 
            arglist[2] = 1; 
            arglist[3] = false; 
            arglist[4] = printName == null ? "" : printName;
            arglist[5] = false; 
            arglist[6] = true; 
            xlSheet.method("PrintOut", arglist); 

//       	 	xlSheet.PrintOut(0, 2, 5, true);
//       	 	xlSheet.PrintOut(0, 2, 3, true,printName,false,true);
            
			//call a default printer to print.
			//xlSheet.PrintOut();

			xlBook.Close(false, null, false);
			if(excel != null){
				excel.Quit();
			}

		}catch(JComException e){
			e.printStackTrace();
		} finally {
			rm.release();
		}
	}
	
	private static void checkFileType(String fileName) throws IOException{
		// check file
		if (fileName != null && !fileName.toLowerCase().endsWith(FILE_TYPE)) {
			throw new IOException("the file is not excel 2003 type !");
		}
	}
	
//	public static void main(String[] args) {
//		try {
//			PrintExcelByJcom.printPerview("D:\\CrazyCode\\MSAS-Print\\templet\\msas-card-print-templet.xls", "1");
////			PrintExcelByJcom.printExcel("D:\\CrazyCode\\MSAS-Print\\templet\\msas-card-print-templet.xls", "1","FinePrint");
//			System.out.println("out");
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
//	}
	public static void main(String[] args) {

		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		System.out.println(cal.get(Calendar.YEAR) );
		System.out.println(cal.get(Calendar.MONTH) );
		System.out.println(cal.get(Calendar.DAY_OF_MONTH) );
	}
}
